<?php

namespace loggedin\model;

require_once("/loggedin/model/Question.php");

class AlterQuestionModel {

	/**
	 * [changeQuestion description]
	 * @param  Question $question
	 * @param  int   $questionID
	 * @param  mysqli   $mysqli
	 * @throws Exception om något går fel i databasen.
	 */
	public function changeQuestion(Question $question, $questionID, \mysqli $mysqli) {
		$sqlQ = "UPDATE `Questions`
				SET `Question` = ?, `RightAnswer` = ?, `WrongAnswer1` = ?,
					`WrongAnswer2` = ?, `WrongAnswer3` = ?
				WHERE `QuestionID` = $questionID;";

		$stmt = $mysqli->prepare($sqlQ);
		if($stmt == false) {
			throw new \Exception("Prepare of sql question failed" . $mysqli->error);
		}

		$questionString = $question->getQuestion();
		$rightAnswer = $question->getRightAnswer();
		$wrongAnswer1 = $question->getWrongAnswer1();
		$wrongAnswer2 = $question->getWrongAnswer2();
		$wrongAnswer3 = $question->getWrongAnswer3();

		$res = $stmt->bind_param("sssss", $questionString, $rightAnswer, $wrongAnswer1,
								 $wrongAnswer2, $wrongAnswer3);
		if ($res == false) {
			throw new \Exception("Bind param failed " . $stmt->error);
		}
		$res = $stmt->execute();
		if($res == false) {
			throw new \Exception("Database execute failed " . $stmt->error);
		}
	}
}